package com.oocl.customer.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.customersystem.service.CustomerManager;
import com.oocl.customersystem.service.MUserManager;
import com.oocl.customersystem.service.impl.CustomerManagerImpl;
import com.oocl.customersystem.service.impl.MUserManagerImpl;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerManager cManager;

	public LoginServlet() {
		super();
		cManager = new CustomerManagerImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}


	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cname = request.getParameter("cname");
		String pwd = request.getParameter("pwd");
		

		// check
		boolean isError = false;
		if (cname == null || "".equals(cname)) {
			request.setAttribute("nameMsg", "name can not be emtry");
			isError = true;
		}
		if (pwd == null || "".equals(pwd)) {
			request.setAttribute("pwdMsg", "password can not be emtry");
			isError = true;
		}

		if (isError) {
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
			return;
		}
		if (cManager.CustomerLogin(cname, pwd)) {
			request.getSession().setAttribute("cname", cname);
			request.getRequestDispatcher("showAllMerchant.jsp").forward(request, response);			 
		} else {
			response.sendRedirect("error.jsp");
			return;
		}
	}

}